import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import monkey, { cdn, util } from 'vite-plugin-monkey'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    monkey({
      entry: 'src/main.ts',
      userscript: {
        name: {
          '': 'Bilibili Live Tasks Helper',
          en: 'Bilibili Live Tasks Helper',
          zh: 'Bilibili Live Tasks Helper',
        },
        namespace: 'https://github.com/andywang425',
        author: 'andywang425',
        description: {
          '': 'Enhancing the experience of watching Bilibili live streaming.',
          en: 'Enhancing the experience of watching Bilibili live streaming.',
          zh: '增强Bilibili直播观看体验。',
        },
        homepageURL: 'https://github.com/andywang425/BLTH',
        supportURL: 'https://github.com/andywang425/BLTH/issues',
        updateURL:
          'https://raw.githubusercontent.com/andywang425/BLTH/master/dist/bilibili-live-tasks-helper.meta.js',
        downloadURL:
          'https://raw.githubusercontent.com/andywang425/BLTH/master/dist/bilibili-live-tasks-helper.min.user.js',
        icon: '',
        copyright: '2025, andywang425 (https://github.com/andywang425)',
        license: 'MIT',
        match: [
          '*://live.bilibili.com/1*',
          '*://live.bilibili.com/2*',
          '*://live.bilibili.com/3*',
          '*://live.bilibili.com/4*',
          '*://live.bilibili.com/5*',
          '*://live.bilibili.com/6*',
          '*://live.bilibili.com/7*',
          '*://live.bilibili.com/8*',
          '*://live.bilibili.com/9*',
          '*://live.bilibili.com/blanc/1*',
          '*://live.bilibili.com/blanc/2*',
          '*://live.bilibili.com/blanc/3*',
          '*://live.bilibili.com/blanc/4*',
          '*://live.bilibili.com/blanc/5*',
          '*://live.bilibili.com/blanc/6*',
          '*://live.bilibili.com/blanc/7*',
          '*://live.bilibili.com/blanc/8*',
          '*://live.bilibili.com/blanc/9*',
        ],
        'run-at': 'document-start',
        connect: [
          'api.bilibili.com',
          'api.live.bilibili.com',
          'api.vc.bilibili.com',
          'passport.bilibili.com',
          'live.bilibili.com',
          'live-trace.bilibili.com',
        ],
      },
      build: {
        externalGlobals: {
          vue: cdn.unpkg('Vue', 'dist/vue.global.prod.js').concat(
            // For pinia, another solution is .concat('https://unpkg.com/vue-demi@latest/lib/index.iife.js')
            util.dataUrl(';window.Vue=Vue;window.VueDemi=Vue;'),
          ),
          'element-plus': cdn.unpkg('ElementPlus', 'dist/index.full.min.js'),
          '@element-plus/icons-vue': cdn.unpkg('ElementPlusIconsVue', 'dist/index.iife.min.js'),
          pinia: cdn.unpkg('Pinia', 'dist/pinia.iife.prod.js'),
          'vue-draggable-plus': cdn.unpkg('VueDraggablePlus', 'dist/vue-draggable-plus.iife.js'),
          lodash: cdn.unpkg('_', 'lodash.min.js'),
          'hotkeys-js': cdn.unpkg('hotkeys', 'dist/hotkeys.min.js'),
          luxon: cdn.unpkg('luxon', 'build/global/luxon.min.js'),
          'crypto-js': cdn.unpkg('CryptoJS', 'crypto-js.js'),
        },
        externalResource: {
          'element-plus/dist/index.css': cdn.unpkg(),
        },
        metaFileName: true,
      },
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
})
